package com.haocang.szgm.app;

import android.app.Application;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.haocang.szgm.utils.config.Config;
import com.haocang.szgm.utils.crashhandler.CrashHandler;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.message.IUmengRegisterCallback;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengNotificationClickHandler;
import com.umeng.message.entity.UMessage;

import net.tsz.afinal.FinalDb;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * 创建时间：2019/3/4
 * 编 写 人：ShenC
 * 功能描述：程序入口
 */

public class MyApplicstion extends Application {
    private static MyApplicstion myApplicstion =null;
    private static Context context;


    @Override
    public void onCreate() {
        super.onCreate();
        myApplicstion = this;
        context = getApplicationContext();

        UMConfigure.init(myApplicstion, "5cbd60aa61f5642ed3001440", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "e5334e2c8eab450f625d911ad9edf440");

        PushAgent mPushAgent = PushAgent.getInstance(myApplicstion);
        //注册推送服务，每次调用register方法都会回调该接口
        mPushAgent.register(new IUmengRegisterCallback() {
            @Override
            public void onSuccess(String deviceToken) {
                //注册成功会返回deviceToken deviceToken是推送消息的唯一标志
                Log.i(Config.LogFlag,"注册成功：deviceToken：-------->  " + deviceToken);
            }
            @Override
            public void onFailure(String s, String s1) {
                Log.e(Config.LogFlag,"注册失败：-------->  " + "s:" + s + ",s1:" + s1);
            }
        });

        UmengNotificationClickHandler notificationClickHandler = new UmengNotificationClickHandler(){

            @Override
            public void dealWithCustomAction(Context context, UMessage msg){
                Toast.makeText(context,msg.custom,Toast.LENGTH_LONG).show();
                Log.e(Config.LogFlag,msg.custom);
            }

        };

        mPushAgent.setNotificationClickHandler(notificationClickHandler);

    }

    public static  MyApplicstion  getInstance(){
        return myApplicstion;
    }

    public static Context getAppContext()
    {
        return context;
    }


    public static void initConfig(){

        //初始化数据库
        Config.db = FinalDb.create(myApplicstion, "szgm.db", true, Config.db_version,new FinalDb.DbUpdateListener() {

            public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
                // TODO Auto-generated method stub
                try {
                    InputStream in = myApplicstion.getAssets().open("patch.sql");
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
                    String sqlUpdate = null;
                    while ((sqlUpdate = bufferedReader.readLine()) != null) {
                        if (!TextUtils.isEmpty(sqlUpdate)) {
                            db.execSQL(sqlUpdate);
                        }
                    }
                    bufferedReader.close();
                    in.close();
                } catch (SQLException e) {
                    System.out.println(e.toString());
                } catch (IOException e) {
                    System.out.println(e.toString());
                }
            }
        });

        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(myApplicstion);




    }


    @Override
    public void onTerminate() {
        super.onTerminate();
    }

}
